#include "sceneMain.h"
#include "sceneTitle.h"
#include "player.h"
#include "enemy.h"
#include "../spawner.h"
#include "../world/effect.h"
#include "../screen/ui_mouse.h"
#include "../world/spell.h"
#include "../screen/hud_stats.h"
#include "../screen/hud_text.h"
#include "../screen/hud_button.h"
#include "../stats/timer.h"
#include "../stats/bg_star.h"
#include <fstream>
void sceneMain::init()
{
    SDL_HideCursor();
    game_.playMusic("assets/bgm/OhMyGhost.ogg");
    world_size = game_.getScreenSize() * 3.0f;
    camera_position = world_size / 2.0f - game_.getScreenSize() / 2.0f;
    py = new player();
    py->init();
    py->setWorldPosition(world_size / 2.0f);
    py->name_ = "player";
    addChild(py);

    spawn = new spawner();
    spawn->init();
    spawn->setTarget(py);
    addChild(spawn);

    deadTime = timer::createTimer(this,3.0f);
    bg_star::createBgStar(this,2000,0.2,0.5,0.7);

    mouse = uiMouse::createUiMouse(this,"./assets/UI/29.png","./assets/UI/30.png",1.0f,Anchor::ANCHOR_CENTER);
    mouse->name_ = "mouse";
    hub = hud_stats::createHubStats(this,py,glm::vec2(30.0f));
    textScore = hudText::createHUDText(this,"Score: 0","textScore",
        glm::vec2(game_.getScreenSize().x - 120.0f,30.0f),glm::vec2(200.0f,50.0f));
    pause = hudButton::createHUDButton(this,game_.getScreenSize() - glm::vec2(230.0f,30.0f),"pause",
        "assets/UI/A_Pause1.png",
        "assets/UI/A_Pause2.png",
        "assets/UI/A_Pause2.png");
    restart = hudButton::createHUDButton(this,game_.getScreenSize() - glm::vec2(140.0f,30.0f),"restart",
        "assets/UI/A_Restart1.png",
        "assets/UI/A_Restart2.png",
        "assets/UI/A_Restart3.png");
    back = hudButton::createHUDButton(this,game_.getScreenSize() - glm::vec2(50.0f,30.0f),"back",
        "assets/UI/A_Back1.png",
        "assets/UI/A_Back2.png",
        "assets/UI/A_Back3.png");
}

void sceneMain::clean()
{   
    scene::clean();
}

void sceneMain::render()
{
    //SDL_Log("rendering sceneMain");
    renderBackground();
    scene::render();
}

void sceneMain::update(float dt)
{
    //camera_position += glm::vec2(100.0f, 100.0f) * dt;
    checkSlowDown(dt);
    scene::update(dt);
    updateScore();
    checkBack();
    checkRestart();
    checkPause();
    if(py && !py->isActive()){
        deadTime->start();
    }
    checkEndTime();
    //SDL_LogError
}

bool sceneMain::handleEvents(SDL_Event& event)
{
    if(scene::handleEvents(event)) return true;
    return false;
}

void sceneMain::updateScore()
{
    textScore->setText((std::string("Score: ") + std::to_string(game_.getScore())).c_str());
}

void sceneMain::checkSlowDown(float& dt)
{
    if(game_.getMouseButton() & SDL_BUTTON_RMASK){
        dt *=0.4f;
    }
}

void sceneMain::checkPause()
{
    if(!pause->getIsTrigger())return;
    if(isPause)
        Resume();
    else 
        Pause();
}

void sceneMain::checkRestart()
{
    if(restart->getIsTrigger()){
        game_.setScore(0);
        saveData("assets/score.dat");
        game_.safeChangeScene(new sceneMain());
    }
}

void sceneMain::checkBack()
{
    if(back->getIsTrigger()){
        game_.setScore(0);
        saveData("assets/score.dat");
        game_.safeChangeScene(new sceneTitle());
    }
}

void sceneMain::checkEndTime()
{
    if(!deadTime->TimeOut())return;
    Pause();
    game_.resumeMusic();
    restart->setRenderPosition(game_.getScreenSize() / 2.0f - glm::vec2(200.0f,0.0f));
    restart->setScale(4.0f);
    back->setRenderPosition(game_.getScreenSize() / 2.0f + glm::vec2(200.0f,0.0f));
    back->setScale(4.0f);
    pause->setActive(false);
    saveData("assets/score.dat");
    deadTime->stop(); 
}

void sceneMain::saveData(const char *filename)
{
    std::ofstream file(filename,std::ios::binary);
    if(!file.is_open()){
        SDL_Log("Error","Can't open file %s",filename);
        return;
    }
    SDL_Log("score %d, highscore %d",game_.getScore(),game_.getHighScore());
    auto score = game_.getHighScore();
    file.write(reinterpret_cast<char*>(&score),sizeof(game_.getScore()));
    file.close();
}

void sceneMain::renderBackground()
{
    auto start = - camera_position;
    auto end = world_size - camera_position;
    game_.drawGrid(start, end, 80.0f, {0.5f,0.5f,0.5f,1.0f});
    game_.drawBoundray(start, end, 5.0f, {0.5f,0.5f,0.5f,1.0f});
}
